/*
* Copyright (c) 2012 Kyle Hayward
* 
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* 
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
* 
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#pragma once

namespace OptixDotNet 
{
	ref class Program;
	ref class Variable;
	ref class Transform;
	ref class Group;
	ref class GeometryGroup;

	/// <summary>
	/// A programmable hierarchy node that selects which children to traverse.
	/// A Selector node can contain a collection of: <see cref="OptixDotNet::Group">Group</see>, 
	/// <see cref="OptixDotNet::GeometryGroup">GeometryGroup</see>, <see cref="OptixDotNet::Selector">Selector</see>, 
	/// or <see cref="OptixDotNet::Transform">Transform</see> nodes.
    /// </summary>
	public ref class Selector : public ContainerNode, IContainerNode<ContainerNode^>, INodeCollectionProvider<ContainerNode^>, IVariableContainer
	{
	public:

		/// <summary>
		/// Creates a new Selector node.
		/// </summary>
		Selector( Context^ context );

		/// <summary>
		/// Checks to make sure the object is valid and consistent
        /// </summary>
		virtual void Validate() override
		{
			if( mSelector )
				CheckError( rtSelectorValidate( mSelector ) );

			mSelector = 0;
		}

		/// <summary>
		/// Destroys the internal Optix resource
        /// </summary>
		virtual void Destroy() override
		{
			CheckError( rtSelectorDestroy( mSelector ) );
		}

		/// <summary>
		/// Sets the number of child nodes to be attached to the selector.
        /// </summary>
		property unsigned int ChildCount
		{
			virtual unsigned int get() = IContainerNode<ContainerNode^>::ChildCount::get
			{
				unsigned int count;
				CheckError( rtSelectorGetChildCount( mSelector, &count ) );
				
				return count;
			}
			virtual void set( unsigned int count ) = IContainerNode<ContainerNode^>::ChildCount::set
			{
				CheckError( rtSelectorSetChildCount( mSelector, count ) );
			}
		}

		/// <summary>
		/// Gets the child node object attached to a Selector node at [index].
		/// A node object can be one of: Group, Selector, GeometryGroup, or Transform
        /// </summary>
		/// <param name="index">Index of the node</param>
		ContainerNode^ GetChild( unsigned int index );

		/// <summary>
		/// Attaches a child node object to a Selector node at [index].
		/// A node object can be one of: Group, Selector, GeometryGroup, or Transform
        /// </summary>
		/// <param name="index">Index of the node</param>
		/// <param name="node">node to attach</param>
		void SetChild( unsigned int index, ContainerNode^ node );

		/// <summary>
		/// Attaches a child node object to the end of a Selector node child list
		/// A node object can be one of: Group, Selector, GeometryGroup, or Transform
        /// </summary>
		/// <param name="node">node to add</param>
		void AddChild( ContainerNode^ node )
		{
			SetChild( ChildCount++, node );
		}

		/// <summary>
		/// Gets the NodeCollection containded by the Selector
		/// NodeCollection is comprised of nodes of: Group, Selector, GeometryGroup, or Transform
        /// </summary>
		property NodeCollection<ContainerNode^>^ Children
		{
			virtual NodeCollection<ContainerNode^>^ get() = INodeCollectionProvider<ContainerNode^>::Collection::get
			{
				return mCollection;
			}
		}

		/// <summary>
		/// Sets a visit program that will be executed when a selector node is visited by a ray.
		/// The visit program determins how a selector node's children are selected.
        /// </summary>
		property Program^ VisitProgram
		{
			Program^ get();
			void set( Program^ count );
		}

		/// <summary>
		/// Gets the number of global variables on the Context
        /// </summary>
		property int VariableCount
		{
			virtual int get()
			{
				unsigned int count;
				CheckError( rtSelectorGetVariableCount( mSelector, &count ) );
				return count;
			}
		}

		/// <summary>
		/// Gets a local variable on the selector with name [s]. If the variable doesn't exist, it is created.
        /// </summary>
		property Variable^ default[System::String^]
		{
			virtual Variable^ get( System::String^ s );
			virtual void set( System::String^s, Variable^ v );
		}

		/// <summary>
		/// Gets a local selector variable at index [index]
        /// </summary>
		property Variable^ default[int]
		{
			virtual Variable^ get ( int index );
			virtual void set( int index, Variable^ v );
		}

	internal:
		Selector( Context^ context, RTselector selector );
		inline RTselector InternalPtr() { return mSelector; }
		virtual RTobject ObjectPtr() override { return InternalPtr(); }

		/// <summary>
		/// Gets or Attaches a child node to a selector at [index]
		/// A node object can be one of: Group, Selector, GeometryGroup, or Transform
        /// </summary>
		property ContainerNode^ ChildNodes[unsigned int]
		{
			virtual ContainerNode^ get ( unsigned int index ) = IContainerNode<ContainerNode^>::ChildNodes::get;
			virtual void set ( unsigned int index, ContainerNode^ node ) = IContainerNode<ContainerNode^>::ChildNodes::set;
		}

	private:

		RTselector	mSelector;
		NodeCollection<ContainerNode^>^ mCollection;
	};
}